package com.kingmed.kmss.admin.modules.sys.service.impl;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kingmed.kmss.admin.modules.sys.mapper.SysBpmnConfigMapper;
import com.kingmed.kmss.admin.modules.sys.model.SysBpmnConfig;
import com.kingmed.kmss.admin.modules.sys.service.SysBpmnConfigService;
import com.kingmed.kmss.admin.modules.sys.vo.BpmnConfigDetailVO;
import com.kingmed.kmss.common.exception.BusinessException;
import com.kingmed.kmss.common.qc.admin.BpmnConfigQC;
import com.kingmed.kmss.common.utils.BeanCopyUtils;
import com.kingmed.kmss.common.vo.admin.SysBpmnConfigVO;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

import static com.kingmed.kmss.common.constant.sys.SysConfigConst.SYS_BPMN_DISCOUNT_CONFIG_KEY;

/**
 * <p>
 * 系统流程配置 服务实现类
 * </p>
 *
 * @author liangzhi
 * @since 2023-10-18
 */
@Service
public class SysBpmnConfigServiceImpl extends ServiceImpl<SysBpmnConfigMapper, SysBpmnConfig> implements SysBpmnConfigService {
    // 集团公司id
    private static final Long GROUP_COMPANY_ID = 0L;
    // 扣率下水道值
    private static final BigDecimal CONDITION_START_VALUE = new BigDecimal("0.00");
    // 扣率天花板值
    private static final BigDecimal CONDITION_END_VALUE = new BigDecimal("1000.00");


    @Override
    public SysBpmnConfigVO getByCompanyIdWithDefault(Long companyId) {
        List<SysBpmnConfig> configList = super.list(new LambdaQueryWrapper<SysBpmnConfig>().in(SysBpmnConfig::getCompanyId, companyId, GROUP_COMPANY_ID));
        if (configList.size() == 1) {
            return BeanCopyUtils.copy(configList.get(0), SysBpmnConfigVO.class);
        }
        if (configList.size() > 2) {
            throw new BusinessException("获取流程配置失败，子公司配置存在多个");
        }
        SysBpmnConfig sysBpmnConfig = configList.stream().filter(c -> !Objects.equals(c.getCompanyId(), GROUP_COMPANY_ID)).findFirst().orElseThrow(() -> new BusinessException("获取流程配置失败！"));
        return BeanCopyUtils.copy(sysBpmnConfig, SysBpmnConfigVO.class);
    }

    @Override
    public Page<SysBpmnConfigVO> pageList(BpmnConfigQC bpmnConfigQC) {
        Page<SysBpmnConfigVO> page = new Page<>(bpmnConfigQC.getPageNo(), bpmnConfigQC.getPageSize());
        return super.getBaseMapper().pageListBpmnConfig(page, bpmnConfigQC);
    }

    @Override
    public BpmnConfigDetailVO getConfigDetail(Long id) {
        SysBpmnConfig config = super.getById(id);
        String configDetail = config.getConfigDetail();
        JSONObject jsonObject = JSONUtil.parseObj(configDetail);
        BpmnConfigDetailVO bpmnConfigDetailVO = new BpmnConfigDetailVO();
        bpmnConfigDetailVO.setBizModeName(jsonObject.getStr("bizModeName"));
        bpmnConfigDetailVO.setConditionName(jsonObject.getStr("conditionName"));
        bpmnConfigDetailVO.setAssistantApproval(jsonObject.getBool("assistantApproval", false));
        String discountConfig = jsonObject.getStr(SYS_BPMN_DISCOUNT_CONFIG_KEY);
        String[] discountStrArr = discountConfig.split(",");
        Set<BigDecimal> discountSet = new TreeSet<>();
        // 转换类型+排序
        for (String discountStr : discountStrArr) {
            BigDecimal discount = new BigDecimal(discountStr);
            discountSet.add(discount);
        }
        bpmnConfigDetailVO.setConfigValueList(this.getConditionValueList(discountSet));
        return bpmnConfigDetailVO;
    }

    /**
     * 组装列表
     */
    private List<BpmnConfigDetailVO.BpmnConditionValue> getConditionValueList(Set<BigDecimal> discountSet) {
        int i = 0;
        BigDecimal prefixValue = null;
        List<BpmnConfigDetailVO.BpmnConditionValue> bpmnConditionValueList = new ArrayList<>(discountSet.size());
        for (BigDecimal discount : discountSet) {
            BpmnConfigDetailVO.BpmnConditionValue bpmnConditionValue = new BpmnConfigDetailVO.BpmnConditionValue();
            // 第一次
            if (i == 0) {
                bpmnConditionValue.setStartValue(CONDITION_START_VALUE);
                bpmnConditionValue.setEndValue(discount);
            } else {
                bpmnConditionValue.setStartValue(prefixValue);
                bpmnConditionValue.setEndValue(discount);
            }
            bpmnConditionValueList.add(bpmnConditionValue);
            // 最后一次
            if (i == discountSet.size() - 1) {
                BpmnConfigDetailVO.BpmnConditionValue lastOneBpmnConditionValue = new BpmnConfigDetailVO.BpmnConditionValue();
                lastOneBpmnConditionValue.setStartValue(discount);
                lastOneBpmnConditionValue.setEndValue(CONDITION_END_VALUE);
                bpmnConditionValueList.add(lastOneBpmnConditionValue);
            }
            prefixValue = discount;
            i++;
        }
        return bpmnConditionValueList;
    }
}
